import random

from django.shortcuts import render,HttpResponse
from django.conf import settings
from app01 import models
# Create your views here.
import os,django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "war_djago.settings")# project_name 项目名称
django.setup()

from utils.tencent.sms import send_sms

def send_sms_one(request):
    #登录验证类型分布
    tpl = request.GET.get('tpl')
    templateid = settings.TENCENT_SMS_TEMPLATE.get(tpl)  #赋值匹配 模板ID
    if not templateid:
        return HttpResponse('请输入正确网址！')

    code_ie = str(random.randrange(100000, 999999))
    code=[]
    code.append(code_ie)
    ph_number_ie= '+86'
    number = '18567185931'
    ph_number=[]
    ph_number.append(ph_number_ie+number)
    #send_sms(ph_number, ['204781', ], '1492850')

    res = send_sms(ph_number,code,'1492850')
    if res==None:
        #print(res)
        return HttpResponse("失败")
    else:
        return HttpResponse("成功")



from django import forms
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
class RegisterModelForm(forms.ModelForm):
    mobile_phone = forms.CharField(label='电话号码',validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$','手机号格式错误')])
    password = forms.CharField(label='密码',widget=forms.PasswordInput(attrs={'class':'form-control'}))   #变为密文
    confirm_password = forms.CharField(label='重复密码',widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'请输入新密码'}))
    code = forms.CharField(label='验证码',widget=forms.TextInput(attrs={'class':'form-control','placeholder':'请输入验证码'}))


    class Meta:
        model = models.UserInfo
        fields = ['username','email','password','confirm_password','mobile_phone','code']
    def __init__(self,*args,**kwargs):
        super().__init__()
        for name,field in self.fields.items():
            field.widget.attrs['class']='form-control'
            field.widget.attrs['placeholder']='请输入%s'%field.label
def register(request):
    """注册页面"""
    form = RegisterModelForm()
    return render(request,'app01/register.html',{'form':form})







